﻿using OpenGameEngine.Scenes;
using OpenGameEngine;
using System.Globalization;
using System;
using System.IO;

namespace ChopperGame.Scenes
{
    public class MenuScene : GameScene
    {
        private RectangleF ScreenBounds;
        private SolidColorBrush BackgroundBrush;
        private SolidColorBrush TextBrush;
        private SolidColorBrush ScoreBrush;
        private Font TextFont;
        private Font ScoreFont;
        private RectangleF TextDrawingRectangle;
        private SolidColorBrush BlackBrush;
        private RectangleF BlackRectangle;
        private Chopper Player;
        private Int64 BestDistance;

        public MenuScene()
            : base("MenuScene")
        {
            this.AcceptsMouseInput = true;
        }

        public override void Load(OpenGameEngine.Base.GameCoreEventArgs e)
        {
            base.Load(e);

            ScreenBounds = new RectangleF(0, 0, e.Core.Config.Width, e.Core.Config.Height);
            BackgroundBrush = new SolidColorBrush(Color.CornflowerBlue);
            ScoreBrush  = new SolidColorBrush(Color.White);
            ScoreFont = new Font("Comic Sans MS", 17f);
            TextBrush = new SolidColorBrush(Color.CornflowerBlue);
            TextFont = new Font("Comic Sans MS", 25f, FontStyle.Normal, FontWeight.UltraBold);
            SizeF Metrics = Font.GetTextSize("Click to start!", TextFont);
            TextDrawingRectangle = new RectangleF(100 + (e.Core.Config.Width / 2) - (Metrics.Width / 2), (e.Core.Config.Height / 2) - (Metrics.Height / 2), Metrics.Width, Metrics.Height);
            BlackBrush = new SolidColorBrush(Color.Black);
            BlackRectangle = new RectangleF(0, 100, e.Core.Config.Width, 400);
            Player = new Chopper(true);
            Player.Load(e);

            String appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            String SaveFilePath = appData + "\\MatthiWare\\ChopperGame\\score.bin";

            FileInfo fi = new FileInfo(SaveFilePath);
            if (!fi.Directory.Exists)
                fi.Directory.Create();

            using (BinaryReader SaveReader = new BinaryReader(File.Open(SaveFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)))
                if (SaveReader.BaseStream.Length > 0)
                    this.BestDistance = SaveReader.ReadInt64();
        }

        public override void Draw(OpenGameEngine.Base.DrawEventArgs e)
        {
            e.FillRectangle(ScreenBounds, BackgroundBrush);

            base.Draw(e);

            e.FillRectangle(BlackRectangle, BlackBrush);

            e.DrawString("Click to start!", TextFont, TextBrush, TextDrawingRectangle);

            Player.Draw(e);

            e.DrawString("Distance: 0", ScoreFont, ScoreBrush, 10, 570);
            String bestDistance = "Best: " + BestDistance.ToString(CultureInfo.CurrentCulture);
            e.DrawString(bestDistance, ScoreFont, ScoreBrush, (800 - Font.GetTextSize(bestDistance, ScoreFont).Width) - 10, 570);
        }

        public override void Update(OpenGameEngine.Base.GameCoreEventArgs e)
        {
            base.Update(e);

            PlayScene playScene = (PlayScene)(e.Core.Scenes["PlayScene"]);
            if (playScene.CurrentLevel != null)
            {
                BestDistance = playScene.CurrentLevel.BestDistance;
            }

            Player.Update(e);
        }

        public override void MouseClick(OpenGameEngine.Base.GameCoreEventArgs e, System.Windows.Forms.MouseButtons button)
        {
            base.MouseClick(e, button);

            e.Core.SwitchScene("PlayScene");
        }


    }
}
